package com.douqu.game.auth.web.controller;

import com.douqu.game.auth.database.model.*;
import com.douqu.game.auth.service.TDService;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.web.response.BaseResponseDto;
import com.douqu.game.core.web.response.SuccessResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * http://doc.talkingdata.com/posts/163
 */
@RestController
public class TalkingDataController {
    @Autowired
    private TDService tdService;

    /**
     * 激活
     */
    @RequestMapping(value = "/td/activation",method = RequestMethod.GET,produces = "application/json;charset-UTF-8")
    public BaseResponseDto avtivation(String appkey, long eventTime, String osversion, String idfa, String mac, String advertisingid, String androidid, String imei,
                                      String tdid, String ip, String adnetname, String channelPackageId, HttpServletRequest request, HttpServletResponse response){
        LogUtils.debug("收到激活回调");
        TDActivationModel model = new TDActivationModel();
        model.setAppkey(appkey);
        model.setEventTime(eventTime);
        model.setOsversion(osversion);
        model.setIdfa(idfa);
        model.setMac(mac);
        model.setAdvertisingid(advertisingid);
        model.setAndroidid(androidid);
        model.setTdid(tdid);
        model.setIp(ip);
        model.setAdnetName(adnetname);
        model.setChannelPackageId(channelPackageId);
        tdService.avtivation(model);
        return new SuccessResponseDto();
    }

    /**
     * 注册
     * @param account
     * @param appkey
     * @param adnetName
     * @param channelPackageId
     * @param request
     * @param response
     */
    @RequestMapping(value = "/td/register",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public BaseResponseDto register(String account,String appkey,long eventTime,String osversion,String deviceType,String idfa,String mac,String advertisingId,
                         String androidid,String imei,String tdid,String ip,String adnetName,String channelPackageId,String deepLink,
                         HttpServletRequest request, HttpServletResponse response){
        LogUtils.debug("收到注册回调");
        TDRegisterModel model = new TDRegisterModel();
        model.setAppkey(appkey);
        model.setEventTime(eventTime);
        model.setOsversion(osversion);
        model.setDeviceType(deviceType);
        model.setIdfa(idfa);
        model.setMac(mac);
        model.setAdvertisingId(advertisingId);
        model.setAndroidid(androidid);
        model.setImei(imei);
        model.setTdid(tdid);
        model.setIp(ip);
        model.setAdnetName(adnetName);
        model.setChannelPackageId(channelPackageId);
        model.setDeepLink(deepLink);
        tdService.register(account,model);
        return new SuccessResponseDto();
    }

    /**
     * 创建角色回调
     * @param account
     * @param appid
     * @param eventTime
     * @param osversion
     * @param deviceType
     * @param idfa
     * @param mac
     * @param advertisingId
     * @param androidid
     * @param imei
     * @param tdid
     * @param ip
     * @param roleName
     * @param adentName
     * @param channelPackageId
     * @param deeplink
     */
    @RequestMapping(value = "/createRole",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public BaseResponseDto createRole(String account, String appid, long eventTime, String osversion, String deviceType,
                                   String idfa, String mac, String advertisingId, String androidid, String imei,
                                   String tdid, String ip, String roleName, String adentName, String channelPackageId,
                                   String deeplink, HttpServletRequest request, HttpServletResponse response){
        LogUtils.debug("收到创建角色回调");
        TDCreateRoleModel tdCreateRoleModel = new TDCreateRoleModel();
        tdCreateRoleModel.setAppid(appid);
        tdCreateRoleModel.setEventTime(eventTime);
        tdCreateRoleModel.setOsversion(osversion);
        tdCreateRoleModel.setDeviceType(deviceType);
        tdCreateRoleModel.setIdfa(idfa);
        tdCreateRoleModel.setMac(mac);
        tdCreateRoleModel.setAdvertisingId(advertisingId);
        tdCreateRoleModel.setImei(imei);
        tdCreateRoleModel.setTdid(tdid);
        tdCreateRoleModel.setIp(ip);
        tdCreateRoleModel.setRoleName(roleName);
        tdCreateRoleModel.setAdnetName(adentName);
        tdCreateRoleModel.setChannelPackageId(channelPackageId);
        tdCreateRoleModel.setDeepLink(deeplink);
        tdService.createRole(account,tdCreateRoleModel);
        return new SuccessResponseDto();
    }

    /**
     * 登录回调
     * @param account
     * @param appkey
     * @param eventTime
     * @param osversion
     * @param deviceType
     * @param idfa
     * @param mac
     * @param advertisingId
     * @param androidid
     * @param imei
     * @param tdid
     * @param ip
     * @param adnetName
     * @param channelPackageId
     * @param deepLink
     */
    @RequestMapping(value = "/td/login",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public BaseResponseDto login(String account,String appkey,long eventTime,String osversion,String deviceType,String idfa,String mac,String advertisingId,
                      String androidid,String imei,String tdid,String ip,String adnetName,String channelPackageId,String deepLink,
                      HttpServletRequest request,HttpServletResponse response){
        LogUtils.debug("登录回调");
        TDLoginModel model = new TDLoginModel();
        model.setAppkey(appkey);
        model.setEventTime(eventTime);
        model.setOsversion(osversion);
        model.setDeviceType(deviceType);
        model.setIdfa(idfa);
        model.setMac(mac);
        model.setAdvertisingId(advertisingId);
        model.setAndroidid(androidid);
        model.setIemi(imei);
        model.setTdid(tdid);
        model.setIp(ip);
        model.setAdnetName(adnetName);
        model.setChannelPackageId(channelPackageId);
        model.setDeepLink(deepLink);

        tdService.login(account,model);
        return new SuccessResponseDto();
    }

    /**
     * 支付回调
     * @param account
     * @param appkey
     * @param eventTime
     * @param osversion
     * @param deviceType
     * @param idfa
     * @param mac
     * @param advertisingId
     * @param androidid
     * @param imei
     * @param tdid
     * @param ip
     * @param orderid
     * @param amount
     * @param currencyType
     * @param adnetName
     * @param channelPackageId
     * @param deepLink
     * @param request
     * @param response
     */
    @RequestMapping(value = "/td/pay",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public BaseResponseDto pay(String account,String appkey,long eventTime,String osversion,String deviceType,String idfa,String mac,String advertisingId,
                    String androidid,String imei,String tdid,String ip,String orderid,long amount,String currencyType,String adnetName,
                    String channelPackageId,String deepLink,HttpServletRequest request,HttpServletResponse response){
        LogUtils.debug("支付回调");
        TDPayModel model = new TDPayModel();
        model.setAccount(account);
        model.setAppkey(appkey);
        model.setEventTime(eventTime);
        model.setOsversion(osversion);
        model.setDeviceType(deviceType);
        model.setIdfa(idfa);
        model.setMac(mac);
        model.setAdvertisingId(advertisingId);
        model.setAndroidid(androidid);
        model.setImei(imei);
        model.setTdid(tdid);
        model.setIp(ip);
        model.setOrderid(orderid);
        model.setAmount(amount);
        model.setCurrencyType(currencyType);
        model.setAdnetName(adnetName);
        model.setChannelPackageId(channelPackageId);
        model.setDeepLink(deepLink);

        tdService.pay(model);
        return new SuccessResponseDto();
    }

    /**
     * 广告点击
     * @param spreadUrl
     * @param spreadName
     * @param ua
     * @param clickIP
     * @param appid
     * @param adnetName
     * @param request
     * @param response
     */
    @RequestMapping(value = "/td/adClick")
    public BaseResponseDto adClick(String spreadUrl,String spreadName,String ua,String clickIP,String appid,String adnetName,
                        HttpServletRequest request,HttpServletResponse response){
        LogUtils.debug("广告点击回调");
        TDAdClickModel model = new TDAdClickModel();
        model.setSpreadUrl(spreadUrl);
        model.setSpreadName(spreadName);
        model.setUa(ua);
        model.setClickIP(clickIP);
        model.setAppid(appid);
        model.setAdnetName(adnetName);

        tdService.adClick(model);
        return new SuccessResponseDto();
    }
}
